package ru.ssau.customerManagement.controller;

import java.util.Collections;
import java.util.List;
import ru.ssau.customerManagement.model.Model;
import ru.ssau.customerManagement.model.ModelImpl;
import ru.ssau.customerManagement.model.entities.Customer;
import ru.ssau.customerManagement.model.entities.CustomerLocation;

/**
 * Author: illabefat
 * Time: 02.11.2014
 */
public class ServerSideControllerImpl implements Controller{
    private Model model = new ModelImpl();
    
    public ServerSideControllerImpl(Model model) {
        this.model = model;
    }

    public Customer getCustomer(Long id)
    {
        return model.getCustomer(id);
    }

    public void addCustomer(Customer customer) throws CreationException
    {
        if (customer == null) return;
        if (model.getCustomer(customer.getId()) != null)
        {
            throw new CreationException("Customer with id=" + customer.getId() + " already exists");
        }
        model.addCustomer(customer);
    }

    public void replaceCustomer(Customer newCustomer)
    {
        if (newCustomer == null) return;
        model.changeCustomer(newCustomer);
    }

    public void deleteCustomer(Long customerId)
    {
        model.removeCustomer(customerId);
        List<CustomerLocation> locations =  model.getCustomerLocationsByCustomer(customerId);
        for (CustomerLocation location : locations)
        {
            model.removeCustomerLocation(location.getId());
        }
    }

    public void addCustomerLocation(CustomerLocation location) throws CreationException
    {
        if (location == null) return;
        if (model.getCustomerLocation(location.getId()) != null)
        {
            throw new CreationException("CustomerLocation with id=" + location.getId() + " already exists");
        }
        model.addCustomerLocation(location);
    }

    public void replaceCustomerLocation(CustomerLocation location)
    {
        if (location == null) return;
        model.changeCustomerLocation(location);
    }

    public CustomerLocation getLocation(Long id)
    {
        return model.getCustomerLocation(id);
    }

    public void deleteCustomerLocation(Long customerLocationId)
    {
        model.removeCustomerLocation(customerLocationId);
    }

    public List<CustomerLocation> getLocationsByCustomer(Long customerId)
    {
        if (customerId == null)
            return Collections.emptyList();
        return model.getCustomerLocationsByCustomer(customerId);
    }

    public List<Customer> getCustomers()
    {
        return model.getCustomers();
    }


    public boolean tryLockCustomer(Long id) {
        return model.lockCustomer(id);
    }


    public void unlockCustomer(Long id) {
        model.unlockCustomer(id);
    }


    public boolean tryLockCustomerLocation(Long id) {
        return model.lockCustomerLocation(id);
    }


    public void unlockCustomerLocation(Long id) {
        model.unlockCustomerLocation(id);
    }

    @Override
    public List<Customer> findCustomersByName(String name) {
        return model.findCustomersByName(name);
    }

    @Override
    public List<CustomerLocation> findCustomerLocationsByAddress(String address) {
        return model.findCustomerLocationsByAddress(address);
    }

    @Override
    public List<CustomerLocation> getAllLocations() {
        return model.getAllLocations();
    }


}
